﻿Imports Microsoft.WindowsMobile.PocketOutlook
Imports Microsoft.WindowsMobile.PocketOutlook.MessageInterception

Public Class Outlook

    Public Shared ListCategories As Categories 'Shared, Performance issue

    Public Function GetContacts() As String
        Dim oContacts As New Contacts
        Using objOL As New Microsoft.WindowsMobile.PocketOutlook.OutlookSession
            Dim ListContacts = From u As Microsoft.WindowsMobile.PocketOutlook.Contact In objOL.Contacts.Items _
                              Select New Contact With {.WorkFax = u.BusinessFaxNumber, .WorkPhone = u.BusinessTelephoneNumber, _
                                                  .Category = u.Properties(Microsoft.WindowsMobile.PocketOutlook.ContactProperty.Categories).ToString, .Company = u.CompanyName, .Country = u.HomeAddressCountry, _
                                                  .Department = u.Department, .MainEmail = u.Email1Address, .FileAs = u.FileAs, .FullName = u.FirstName & " " & u.LastName, _
                                                  .HomePhone = u.HomeTelephoneNumber, .ItemId = u.ItemId.ToString, .JobTitle = u.JobTitle, .Mobile = u.MobileTelephoneNumber, .Picture = u.Picture, _
                                                  .Nikename = u.Nickname, .WebPage = If(u.WebPage Is Nothing, "", u.WebPage.AbsoluteUri)}

            oContacts.Rows = ListContacts.OrderBy(Of String)(Function(p) p.FullName).ToList
        End Using
        oContacts.Flattern()

        If ListCategories Is Nothing Then
            ListCategories = New Categories
            ListCategories.Rows = (From u In oContacts.Rows _
                                   Where Not String.IsNullOrEmpty(u.Category) _
                                  Select New Category With {.Category = u.Category, .CategoryId = u.Category}).ToList

            Dim oCategoryCompare As New CategoryCompare
            ListCategories.Rows = ListCategories.Rows.Distinct(oCategoryCompare).ToList
        End If

        oContacts.Success = True
        Return ToJson(oContacts)
    End Function

    Public Function GetCategories() As String
        Return ToJson(ListCategories)
    End Function

    Public Function GetAppointments() As String
        Dim objAppointemnts As New Appointments
        Using objOL As New Microsoft.WindowsMobile.PocketOutlook.OutlookSession
            objAppointemnts.Rows = (From u As Microsoft.WindowsMobile.PocketOutlook.Appointment In objOL.Appointments.Items _
                            Order By u.Start _
                            Select New Appointment With {.AllDaysEvent = u.AllDayEvent, .Body = u.Body, .BusyStatus = u.BusyStatus.ToString, _
                                                         .Categories = u.Categories, .Duration = u.Duration, .End = u.End, .ItemId = u.ItemId.ToString, _
                                                         .Location = u.Location, .Start = u.Start, .Subject = u.Subject}).ToList

        End Using
        objAppointemnts.Success = True
        Return ToJson(objAppointemnts)
    End Function

    Public Function GetTasks() As String
        Dim objTasks As New Tasks
        Using objOL As New Microsoft.WindowsMobile.PocketOutlook.OutlookSession
            objTasks.Rows = (From u As Microsoft.WindowsMobile.PocketOutlook.Task In objOL.Tasks.Items _
                            Order By u.StartDate _
                            Select New Task With {.Body = u.Body, .Categories = u.Categories, .Complete = u.Complete, _
                                                  .DueDate = u.DueDate.ToString, .Importance = u.Importance.ToString, .ItemId = u.ItemId.ToString, _
                                                  .Sensivitty = u.Sensitivity.ToString, .StartDate = u.StartDate.ToString, .Subject = u.Subject}).ToList

        End Using
        objTasks.Success = True
        Return ToJson(objTasks)
    End Function

    Private Class CategoryCompare
        Implements Generic.IEqualityComparer(Of Category)

        Public Function Equals1(ByVal x As Category, ByVal y As Category) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of Category).Equals
            Return CBool(x.CategoryId = y.CategoryId)
        End Function

        Public Function GetHashCode1(ByVal obj As Category) As Integer Implements System.Collections.Generic.IEqualityComparer(Of Category).GetHashCode
            Return obj.CategoryId.GetHashCode
        End Function
    End Class

    Public Class Category

        Private _CategoryId As String
        Public Property CategoryId() As String
            Get
                Return _CategoryId
            End Get
            Set(ByVal value As String)
                _CategoryId = value
            End Set
        End Property

        Private _Category As String
        Public Property Category() As String
            Get
                Return _Category
            End Get
            Set(ByVal value As String)
                _Category = value
            End Set
        End Property
    End Class

    Public Class Categories
        Private _Success As Boolean = True
        Public Property Success() As Boolean
            Get
                Return _Success
            End Get
            Set(ByVal value As Boolean)
                _Success = value
            End Set
        End Property

        Public ReadOnly Property Results() As Long
            Get
                Return Me.Rows.Count
            End Get
        End Property

        Private _Rows As List(Of Category)
        Public Property Rows() As List(Of Category)
            Get
                Return _Rows
            End Get
            Set(ByVal value As List(Of Category))
                _Rows = value
            End Set
        End Property

    End Class

    Public Function GetContactPhoto(ByVal ItemId As String) As System.Drawing.Image
        Using objOL As New Microsoft.WindowsMobile.PocketOutlook.OutlookSession
            Dim Contact = (From u As Microsoft.WindowsMobile.PocketOutlook.Contact In objOL.Contacts.Items _
                          Where u.ItemId.ToString = ItemId _
                          Select u).Single
            Return Contact.Picture
        End Using
    End Function

    Public Function DeleteContact(ByVal ItemId As String) As String
        Dim GJson As New GenericJson
        Try
            Using objOL As New Microsoft.WindowsMobile.PocketOutlook.OutlookSession
                Dim DeletedContact = (From u As Microsoft.WindowsMobile.PocketOutlook.Contact In objOL.Contacts.Items _
                                    Where u.ItemId.ToString = ItemId _
                                    Select u).Single
                DeletedContact.Delete()
            End Using
            GJson.Msg = ""
            GJson.Success = True
        Catch ex As Exception
            GJson.Success = False
            GJson.Msg = ex.Message
        End Try
        Return GJson.ToString
    End Function

    Public Function SaveContact(ByVal Contact As Contact) As String
        Dim GJson As New GenericJson
        Try
            Using objOL As New Microsoft.WindowsMobile.PocketOutlook.OutlookSession
                Dim NewContact = objOL.Contacts.Items.AddNew
                If Contact.Anniversary.HasValue Then NewContact.Anniversary = Contact.Anniversary.Value
                If Contact.Birthday.HasValue Then NewContact.Birthday = Contact.Birthday.Value
                NewContact.Body = Contact.Notes
                NewContact.BusinessFaxNumber = Contact.WorkFax
                NewContact.BusinessTelephoneNumber = Contact.WorkPhone
                NewContact.Categories = Contact.Category
                NewContact.CompanyName = Contact.Company
                NewContact.Department = Contact.Department
                NewContact.FirstName = Contact.FirstName
                NewContact.HomeFaxNumber = Contact.HomeFax
                NewContact.HomeTelephoneNumber = Contact.HomePhone
                NewContact.JobTitle = Contact.JobTitle
                NewContact.LastName = Contact.LastName
                NewContact.MobileTelephoneNumber = Contact.Mobile
                NewContact.BusinessAddressCity = Contact.WorkCity
                NewContact.BusinessAddressCountry = Contact.WorkCountry
                NewContact.BusinessAddressPostalCode = Contact.WorkZip
                NewContact.BusinessAddressState = Contact.WorkState
                NewContact.BusinessAddressStreet = Contact.WorkStreet
                NewContact.Email1Address = Contact.MainEmail
                NewContact.HomeAddressCity = Contact.HomeCity
                NewContact.HomeAddressCountry = Contact.HomeCountry
                NewContact.HomeAddressPostalCode = Contact.HomeZip
                NewContact.HomeAddressState = Contact.HomeState
                NewContact.HomeAddressStreet = Contact.HomeStreet
                NewContact.IM1Address = Contact.IM
                NewContact.MiddleName = Contact.Middel

                If Not String.IsNullOrEmpty(Contact.WebPage) Then NewContact.WebPage = New Uri(Contact.WebPage)
                NewContact.Update()
                GJson.Msg = ""
                GJson.Success = True
            End Using
        Catch ex As Exception
            GJson.Success = False
            GJson.Msg = ex.Message
        End Try
        Return GJson.ToString
    End Function

End Class